package com.ihr360.cnb.module.oneclick.controller;

import com.ihr360.cnb.module.oneclick.service.IrenshiDubboProviderService;
import com.ihr360.cnb.module.oneclick.service.OneClickService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.oneclick.entity.Spreadsheet;
import com.ihr360.oneclick.entity.StaffRecord;
import com.ihr360.oneclick.modal.OneClickStaff;
import com.ihr360.oneclick.request.SaveExcelDataRequest;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tom
 * @date 18-1-25
 */
@RestController
@RequestMapping("/oneclick")
@DisableResponseAdvice
public class ExcelDataController {

    @Autowired
    private IrenshiDubboProviderService irenshiDubboProviderService;
    @Autowired
    private OneClickService oneClickService;


    @PostMapping(value = "/excel/save")
    @ApiOperation(value = "/function/staff/month/list", httpMethod = "GET", notes = "Excel 草稿数据保存",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo saveExcelTempData(@RequestBody Spreadsheet spreadsheet, AuthUserInfo authUserInfo) {
        spreadsheet.setCompanyId(authUserInfo.getCompanyId());
        // 获取更新的员工名录list
        ResultInfo<List<StaffRecord>> staffRecordListResult = oneClickService.getExcelImportStaffList(spreadsheet);
        if (staffRecordListResult.getCode() != 0) {
            return staffRecordListResult;
        }

        List<StaffRecord> staffRecordList = staffRecordListResult.getData();
        // 批量注册员工名录
        List<OneClickStaff> oneClickStaffList = new ArrayList<>();
        staffRecordList.forEach(staffRecord -> {
            OneClickStaff oneClickStaff = new OneClickStaff();
            oneClickStaff.setCompanyId(staffRecord.getCompanyId());
            oneClickStaff.setStaffRecordId(staffRecord.getId());
            oneClickStaff.setMobileNo(staffRecord.getMobileNo());
            oneClickStaff.setIdCardNo(staffRecord.getIdCardNo());
            oneClickStaffList.add(oneClickStaff);
        });

        // 调用irenshi注册appUser
        Map<String, String> appUserIdMap = irenshiDubboProviderService.registerStaffRecordAppUser(oneClickStaffList);
        staffRecordList.forEach(staffRecord ->
                staffRecord.setAppUserId(appUserIdMap.get(staffRecord.getMobileNo()))
        );

        // 保存正式excel data 数据
        ResultInfo<Spreadsheet> spreadsheetResult = oneClickService.saveExcelData(new SaveExcelDataRequest(authUserInfo.getUserId(), spreadsheet, staffRecordList));
        return spreadsheetResult;
    }
}
